<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class UploadController extends Controller
{
//    laravel图片上传
    public function upload(Request $request){
//        先默认上传到指定storage目录下
        $file = $request->file('file');
        if ($file->isValid()){
//            注意：这个图片上传是有返回值的，
            $path = $file->store(date('Y-m-d'),'upload');
//            dd($path);
//            图片上传成功，开始解决前面的问题
            return ['file'=>asset('/uploadImages/' . $path),'code'=>0];
        }
    }

//    缩略图的图片写好，写编辑器的图片上传
    public function bianjiqi(Request $request){

        $file = $request->file('file');

        if ($file->isValid()){

            $path = $file->store(date('Y-m-d'),'upload');

            return [

                "success"   => true,
                "msg"       => "上传成功",
                "file_path" => asset('/uploadImages/'.$path)

            ];
        }
    }

//    配置预览多图的图片方法
    public function morepic(){

//        include '../../../vendor/autoload.php';
        $files = glob('uploadImages/*/*');
//        dd($files);
        foreach ($files as $f) {
            $file   = asset($f);
//            dump($f);
            $data[] = [
                'url'        => $file,
                'path'       => $file,
                'size'       => filesize($f),
                'name'       => basename($f),
                'createtime' => date('Y-m-d', filemtime($f)),
            ];
        }
//返回数据 data为文件列表 page 为分页数据，可以使用 houdunwang/page 组件生成
        $json = ['data' => $data,'code'=>0, 'page' => []];
//        die(json_encode($json));
        return $json;
    }
}
